﻿#include <QCoreApplication>
#include <management.h>
#include <QSharedPointer>
#include <tcp_server.h>
#include <enum_type.h>
#include <QTime>
void MessageOutput(QtMsgType type, const QMessageLogContext &context, const QString &msg)
{

    QByteArray localMsg = msg.toLocal8Bit();
    switch (type) {
    case QtDebugMsg:
        fprintf(stderr, "%s\n", localMsg.constData());
        break;
    case QtInfoMsg:
        fprintf(stderr, "Info: %s (%s:%u, %s)\n", localMsg.constData(), context.file, context.line, context.function);
        break;
    case QtWarningMsg:
        fprintf(stderr, "Warning: %s (%s:%u, %s)\n", localMsg.constData(), context.file, context.line, context.function);
        break;
    case QtCriticalMsg:
        fprintf(stderr, "Critical: %s (%s:%u, %s)\n", localMsg.constData(), context.file, context.line, context.function);
        break;
    case QtFatalMsg:
        fprintf(stderr, "Fatal: %s (%s:%u, %s)\n", localMsg.constData(), context.file, context.line, context.function);
        abort();
    }
    QString strDateTime = QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss ddd");
    QString strMessage = QString("DateTime:%5   Message:%1   Function:%4   File:%2   Line:%3")
            .arg(msg).arg(context.file).arg(context.line).arg(context.function).arg(strDateTime);
    // 输出信息至文件中（读写、追加形式）
    if(Common::Log_Open)
    {
        Common::LogdataLock.lockForWrite();
        Common::Logdata.append(strMessage);
        Common::LogdataLock.unlock();
    }
    ///
    /// 没有启动日志会导致应用内存越来越大，因为Common::Logdata没有进行清除!!!
    ///
}

int main(int argc, char *argv[])
{
    bool Log_Open;
    QString SQL_IP,SQL_DBname,SQL_UserName,SQL_Password,LocalPort;
    int TcpHandleThreadNum,TcpPort,Handle_Count,distribution_multiple;
    QCoreApplication a(argc, argv);
    qInstallMessageHandler(MessageOutput);
    QSharedPointer<Management>management=QSharedPointer<Management>(new Management());
    management.data()->Configur_init("balance_server");
    management.data()->StartConfigur();
    Common::ConfigureLock.lockForRead();
    Log_Open=Common::Log_Open;
    SQL_IP=Common::SQL_IP;
    SQL_DBname=Common::SQL_DBname;
    SQL_UserName=Common::SQL_UserName;
    SQL_Password=Common::SQL_Password;
    LocalPort=Common::LocalPort;
    TcpHandleThreadNum=Common::TcpHandleThreadNum;
    TcpPort=Common::TcpPort;
    Handle_Count=Common::Handle_Count;
    distribution_multiple=Common::distribution_multiple;
    Common::ConfigureLock.unlock();

    if(Log_Open)
    {

        management.data()->Log_init("balance_server");
        management.data()->StartLog();
    }
    else
    {
        qInstallMessageHandler(NULL);
    }
    management.data()->mysql_set(SQL_IP,SQL_DBname,SQL_UserName,SQL_Password);
    management.data()->Handle_Setcount(Handle_Count);
    management.data()->Handle_Start();
    management.data()->localServer_init(LocalPort);
    management.data()->localServer_start();
    management.data()->tcpServer_init(TcpHandleThreadNum,TcpPort);
    management.data()->tcpServer_start();
    management.data()->process_init();
    management.data()->process_start();
    qDebug()<<"任务分配倍率:"<<distribution_multiple;
    return a.exec();
}
